﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace MN.Framework.Common
{
    public class TextValidationRule : ValidationRule
    {
        TextOperator textOperator;

        public TextValidationRule(TextOperator textOperator)
        {
            this.textOperator = textOperator;
        }
        public override bool IsValid(BaseEntity record, string propertyName)
        {
            bool flag = false;
            if (string.IsNullOrEmpty(propertyName))
            {
                throw new MnException("Property name cant be null");
            }
            PropertyInfo property = record.PropertyByName(propertyName);
            if (property == null)
            {
                throw new MnException("Property {0} on entity of type {1} not found", new object[] { propertyName, record.GetType().Name });
            }

            if (property.GetValueFast(record) == null)
            {
                return true;
                //throw new MnException("Property {0} is null", property.Name);
            }
            var text = property.GetValueFast(record).ToString();// record.PropertyByName(propertyName).GetValueFast(record).ToString();

            if (string.IsNullOrEmpty(text))
                return true;

            switch (textOperator)
            {
                case TextOperator.Email:
                    flag = regularValidation(text, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z");
                    break;
                case TextOperator.Url:
                    flag = regularValidation(text, @"^http(s)?://([\w-]+.)+[\w-]+(/[\w- ./?%&=])?$");
                    break;
                case TextOperator.NationalCode:
                    flag = regularValidation(text, @"\d{10}");
                    break;
                case TextOperator.PostalCode:
                    flag = regularValidation(text, @"\d{10}");
                    break;
                case TextOperator.PhoneNumber:
                    flag = regularValidation(text, @"\d*(?:\d\D*){7,}$");
                    break;
                case TextOperator.Mobile:
                    flag = regularValidation(text, @"\d{10}") || regularValidation(text, @"\d{11}");
                    break;
                default:
                    flag = false;
                    break;
            }
            if (!flag)
            {
                List<ColumnInfo> columns = new List<ColumnInfo>();
                record.GetColumns(columns);

                base.ErrorMessage = string.Format(Resource.CommonCore.TaxtValidationMessage, columns.Find(c => c.Name == propertyName).HeaderText);
            }
            return flag;
        }
        private bool regularValidation(string text, string rgx)
        {
            return Regex.IsMatch(text, rgx);
        }
    }
}
